package sort;

import java.util.Stack;

public class Sort {
    public static void main(String[] args) {
//        int[] array = {10, 5, 3, 10, 2, 7, 5, 4};
//        int[] array = {2, 1, 4, 3, 5,9,8,6,5,7};
        int[] array = {5, 9, 8, 6, 5, 7};
//        for (int i = 1; i < array.length; i++) {
//            int tmp = array[i];
//            int j = i - 1;
//            for(;j >= 0; j--) {
//                if (array[j] > tmp) {
//                    array[j+1] = array[j];
//                }else {
//                    break;
//                }
//            }
//            array[j+1] = tmp;
//        }
//        insertSort(array);
//         shellSort(array);
//        selectSort(array);
//        selectSort2(array);
//        bubbleSort(array);
//        heapSort(array);
        quickSortNor(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    //归并排序

    /**
     * 时间复杂度：O(n*logN)
     * 空间复杂度：O(N) 与树的高度有关logN
     * 稳定性：稳定
     * 稳定排序 ： 插入 冒泡  归并
     *
     * @param array
     */
    public static void mergeSort(int[] array) {
        mergeFunc(array, 0, array.length - 1);
    }

    private static void mergeFunc(int[] array, int left, int right) {
        if (left >= right) return;
        int mid = left + ((right - left) >> 1);
        mergeFunc(array, left, mid);
        mergeFunc(array, mid + 1, right);
        //两边分解完，开始合并
        merge(array, left, mid, right);
    }

    private static void merge(int[] array, int left, int mid, int right) {
        int s1 = left, e1 = mid, s2 = mid + 1, e2 = right;
        int[] tmpArr = new int[right - left + 1];
        int k = 0;
        // 1.保证s1和s2这两个有序表都是有数据的
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] <= array[s2]) {
                tmpArr[k++] = array[s1++];
            } else {
                tmpArr[k++] = array[s2++];
            }
        }
        // 2.若一个表没有数据了，直接把另一个表剩下的数据全部拷贝到新数组
        while (s1 <= e1) {
            tmpArr[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmpArr[k++] = array[s2++];
        }
        // 3.拷贝到源数组
        for (int i = 0; i < k; i++) {
            array[i + left] = tmpArr[i];
        }

    }

    //非递归
    public static void quickSortNor(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int left = 0, right = array.length - 1;
        int pivot = partition(array, left, right);

        if (pivot - 1 > left) {
            stack.push(left);
            stack.push(pivot - 1);
        }
        if (pivot + 1 < right) {
            stack.push(pivot + 1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            //没有给partition(array,left,right)赋值，导致没有数据输出
            pivot = partition(array, left, right);

            if (pivot - 1 > left) {
                stack.push(left);
                stack.push(pivot - 1);
            }
            if (pivot + 1 < right) {
                stack.push(pivot + 1);
                stack.push(right);
            }
        }
    }


    /**
     * 快排
     *
     * @param array 时间复杂度：
     *              最好情况：O（N*logN)
     *              最坏情况：O(N^2)  有序/逆序
     *              空间复杂度：
     *              最好情况：O（logN) ——>树的高度
     *              最坏情况：O(N)  有序/逆序
     *              稳定性：
     *              不稳定
     */
    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    private static void quick(int[] array, int start, int end) {
        if (start >= end) return;
//2
        if (end - start + 1 <= 10) {
            insertSort2(array, start, end);
            return;
        }

        //1.三数取中法  index中间大的数字下标
        int index = middleNum(array, start, end);
        swap(array, index, start);


        int pivot = partition(array, start, end);
        quick(array, start, pivot - 1);
        quick(array, pivot + 1, end);
    }

    private static int middleNum(int[] array, int left, int right) {
        int mid = left + ((right - left) >> 1);

        if (array[left] > array[right]) {
            if (array[mid] > array[left]) {
                return left;
            } else if (array[mid] < array[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if (array[mid] > array[right]) {
                return right;
            } else if (array[mid] < array[left]) {
                return left;
            } else {
                return mid;
            }
        }
    }

    private static int partitionHoare(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        while (left < right) {
            //right 找比tmp小的
            while (left < right && array[right] >= tmp) {
                right--;
            }
            //left 找比tmp大的
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array, i, left);
        return left;
    }

    //挖坑法
    private static int partition(int[] array, int left, int right) {
//        left = 0;
//        right = array.length-1;
        int tmp = array[left];
        while (left < right) {
            //right找小于tmp
            while (left < right && array[right] >= tmp) {
                right--;
            }
            if (left >= right) break; //这句是优化
            array[left] = array[right];
            //left找大于tmp
            while (left < right && array[left] <= tmp) {
                left++;
            }
            if (left >= right) break;
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    public static void insertSort2(int[] array, int start, int end) {
        for (int i = start + 1; i <= end; i++) {
            int tmp = 0;
            for (int j = i - 1; j >= start; j--) {
                if (array[j] > array[j + 1]) {
                    tmp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = tmp;
                } else {
                    break;
                }

            }
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //冒泡排序
    public static void bubbleSort(int[] array) {
        //i表示的是趟数 10-》 9 趟
        for (int i = 0; i < array.length - 1; i++) {
            //优化
            boolean flg = false;
            for (int j = 0; j < array.length - 1; j++) {
                if (array[j] > array[j + 1]) { //加=就不稳定了
                    swap(array, j, j + 1);
                }
            }
            if (flg == false) {
                return;
            }
        }
    }

    //堆排序  O(N*logN)
    public static void heapSort(int[] array) {
        //创建大根堆
        createHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            swap(array, 0, end);
            shifDown(array, 0, end);
            end--;
        }
    }

    private static void createHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            shifDown(array, parent, array.length);
        }
    }

    private static void shifDown(int[] array, int parent, int len) {
        int child = (2 * parent) + 1;
        while (child < len) {
            if (child + 1 < len && array[child] < array[child + 1]) {
                child = child + 1;
            }
            if (array[child] > array[parent]) {
                swap(array, child, parent);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    //选择排序2
    public static void selectSort2(int[] array) {
        int left = 0, right = array.length - 1;

        while (left < right) {
            int minIndex = left, maxIndex = left;
            int i = left + 1;
            while (i <= right) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                } else if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
                i++;
            }
            if (array[left] > array[minIndex]) {
                int tmp1 = array[left];
                array[left] = array[minIndex];
                array[minIndex] = tmp1;
            }
            //第一次交换的时候， 最大值可能是left,会被换到minIndex的位置
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            if (array[right] < array[maxIndex]) {
                int tmp2 = array[right];
                array[right] = array[maxIndex];
                array[maxIndex] = tmp2;
            }
            left++;
            right--;
        }
    }

    //选择排序1
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            int tmp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = tmp;
        }

    }

    //插入排序
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = 0;
            for (int j = i - 1; j >= 0; j--) {
                if (array[j] > array[j + 1]) {
                    tmp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = tmp;
                } else {
                    break;
                }

            }
        }
    }

    //希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length;
        ;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }

    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            for (int j = i - gap; j >= 0; j--) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = tmp;
                } else {
                    break;
                }
            }
        }
    }

    public static void insertSort1(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    public static int binarySearch(int[] a, int n, int x) {

        if (n > 0 && x >= a[0]) {
            int low = 0, high = n - 1;
            while (low < high) {
                int mid = (low + high + 1) / 2;
//            if (x == a[mid]) return mid;
                if (x >= a[mid]) {
                    low = mid;
                } else {
                    high = mid - 1;
                }

            }
            if (x == a[low]) return low;
        }
        return -1;
    }
}
